$(function(){
	var walletType=sessionStorage.getItem("walletType");
	var logonToken=sessionStorage.getItem("logonToken");
	var playId=sessionStorage.getItem("playId");
	
	
	if(walletType == "0"){
		$(".wallet_type option").eq(1).attr("selected",true);
	}else{
		$(".wallet_type option").eq(2).attr("selected",true);
	}
//	获取数据
 	var json = {
        "logonToken": logonToken,
        "walletType": walletType,
        "playId":playId,
    }
	$.ajax({
		type:"post",
		url:url + '/bigWinner/api/walletType/walletTypePage',
		data:JSON.stringify(json),
		dataType:'json',
		contentType: "application/json;charset=UTF-8",
		success:function(result){
			if(result.success){
				if(result.obj.btcAccount != null){
					$(".btc_inp").val(result.obj.btcAccount.address);
                                        
				}
				if(result.obj.ethAccount != null){
					$(".eth_inp").val(result.obj.ethAccount.address);
                                        
				}
			}else{
				isErr(result);
			}
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) { 
			alert("请求失败："+XMLHttpRequest.status);        
		}
	});
	
	
//	切换币种
	$(".wallet_type").on("change",function(){
		var changeType=$(".wallet_type option:selected").val();
		var json = {
            "logonToken": logonToken,
            "walletType": walletType,
            "playId":playId,
            "changeType":changeType
        }
		$.ajax({
			type:"post",
			url:url + '/bigWinner/api/walletType/changeWalletType',
			data:JSON.stringify(json),
			dataType:'json',
			contentType: "application/json;charset=UTF-8",
			success:function(result){
				if(result.success){
					alert(result.obj.msg);
					sessionStorage.setItem("walletType",result.obj.walletType);
//					切换币种类型名称
					sessionStorage.setItem("BE_type",result.obj.walletType);
					var BE_type=sessionStorage.getItem("BE_type");
					if(BE_type == "0"){
						$(".b0_e1").html("BTC");
                                                window.location.reload();
					}else{
						$(".b0_e1").html("ETH");
                                                window.location.reload();
					}
				}else{
					isErr(result);
					if(walletType == "0"){
						$(".wallet_type option").eq(1).prop("selected",true);
					}else{
						$(".wallet_type option").eq(2).prop("selected",true);
					}
				}
			},
			error:function (XMLHttpRequest, textStatus, errorThrown) { 
				alert("请求失败："+XMLHttpRequest.status);    
			}
		});
	})
	
//	绑定
	var prevent_repetition = true;
	$(".btc_btn").on("click",function(){
		if (prevent_repetition) {
            prevent_repetition = false;
            var btcAddress=$(".btc_inp").val();
            if(btcAddress.length < 26 || btcAddress.length > 35){
				alert("BTC钱包地址不正确，请重新输入");
                prevent_repetition = true;
				return false;
			}else{
				$.ajax({
	                type: "get",
	                url: 'https://chain.api.btc.com/v3/address/'+btcAddress,
	                success: function(result) {
	                	if(result.err_no != 0){
	                		alert("BTC钱包地址不正确，请重新输入");
			                prevent_repetition = true;
							return false;
	                	}
	                	var walletType=sessionStorage.getItem("walletType");
	                	var json = {
			               	"logonToken": logonToken,
				            "walletType": walletType,
				            "playId":playId,
				            "btcAddress":btcAddress
			            }
			            $.ajax({
			                type: "post",
			                url: url + '/bigWinner/api/walletType/bindBTC',
			                data: JSON.stringify(json),
			                dataType: 'json',
			                contentType: "application/json",
			                success: function(result) {
			                    if(result.success){			
									alert(result.msg);
                                                                        window.location.reload();
								}else{
				                    isErr(result);
								}
			                },
			                error: function(XMLHttpRequest, textStatus, errorThrown) {
								alert("请求失败："+XMLHttpRequest.status);    
			                },
			                complete: function() {
			                    prevent_repetition = true;
			                }
			            });
	                },
	                error: function(XMLHttpRequest, textStatus, errorThrown) {
						alert("请求失败："+XMLHttpRequest.status);    
	                },
	                complete: function() {
	                    prevent_repetition = true;
	                }
	            });	
			}
		}
	})
	
	
	$(".eth_btn").on("click",function(){
		if (prevent_repetition) {
            prevent_repetition = false;
            var ethAddress=$(".eth_inp").val();
            var isTrue = web3.isAddress(ethAddress);
            if(!isTrue){
				alert("ETH钱包地址不正确，请重新输入");
                prevent_repetition = true;
                return false;
			}
            var walletType=sessionStorage.getItem("walletType");
        	var json = {
               	"logonToken": logonToken,
	            "walletType": walletType,
	            "playId":playId,
	            "ethAddress":ethAddress
            }
            $.ajax({
                type: "post",
                url: url + '/bigWinner/api/walletType/bindETH',
                data: JSON.stringify(json),
                dataType: 'json',
                contentType: "application/json",
                success: function(result) {
                    if(result.success){			
						alert(result.msg);
                                                window.location.reload();
					}else{
	                    isErr(result);
					}
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert("请求失败："+XMLHttpRequest.status);    
                },
                complete: function() {
                    prevent_repetition = true;
                }
            });
		}
	})
})
